<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Captcha;
use DB;
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $arts = DB::table('arts')->orderBy('id','desc')->paginate(2);
        return view('art.index',['arts'=>$arts]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('art.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except('_token');
        $res = DB::table('arts')->insert($data);
        if($res){
            return redirect('/art');
        }else{
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $art = DB::table('arts')->where('id',$id)->first();
        return view('art.show',['art'=>$art]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $art = DB::table('arts')->where('id',$id)->first();
        return view('art.edit',['art'=>$art]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $res = DB::table('arts')->where('id',$id)->update(['title'=>$request->title,'content'=>$request->content]);
        return redirect('/art');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = DB::table('arts')->where('id',$id)->delete();
        return redirect('/art');
    }

    public function ajax(Request $request, $id)
    {

        $data = $request->all();
        //echo 123;die;
        echo json_encode($data);
       /* return response()->json(array(
            'status' => 1,
            'msg' => 'ok',
        ));*/
        //return view('art.edit',['data'=>$data]);
       //return view('art/edit',123);
    }

    public function getCode()
    {
        dd(Captcha::src());
    }
}
